package zl.osimall.manager.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zl.osimall.common.bean.Order;
import zl.osimall.common.bean.OrderChart;
import zl.osimall.common.bean.OrderDetail;
import zl.osimall.common.bean.RespBean;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.service.OrderService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/page")
    public RespBean selectConditionByPage(Integer pageNum, Integer pageSize) {
        PageInfo<Order> pageInfo = orderService.selectByPage(null ,pageNum, pageSize);
        return RespBean.ok("查询成功", pageInfo);
    }

    @GetMapping("/status")
    public RespBean selectByStatus() {
        List<OrderChart> list = orderService.selectByStatus();
        List<Long> longList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            longList.add(list.get(i).getCount());
        }
        return RespBean.ok("查询成功", longList);
    }

    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id) throws SteduException {
        return RespBean.ok("查询成功",orderService.selectById(id));
    }


    @PutMapping
    public RespBean update( Order order) throws SteduException {
        orderService.update(order);
        return RespBean.ok("修改成功");
    }

    @GetMapping("/detail/{id}")
    public RespBean selectByOrder(@PathVariable("id") String id) throws SteduException {
        return RespBean.ok("查询成功", new  PageInfo<OrderDetail>(orderService.selectByOrder(id)) );
    }

}
